﻿using System;
using System.IO;
using System.Linq;
using LEShortcut.Models.Views;
using Lethe.FileFormat.LNK;

namespace LEShortcut.Helper
{
    /// <summary>
    ///     Provide functions to access shelllink
    /// </summary>
    internal static class ShellLinkHelper
    {
        /// <summary>
        ///     Check the path is a shell link or not
        /// </summary>
        /// <param name="path">Shell link path</param>
        /// <returns></returns>
        internal static bool IsLink(string path)
        {
            // ReSharper disable once PossibleNullReferenceException
            return Path.GetExtension(path).ToUpper() == ".LNK";
        }

        /// <summary>
        ///     Extract the shell link from the path
        /// </summary>
        /// <param name="path">Shell link path</param>
        /// <param name="info">Shell link info object</param>
        /// <returns></returns>
        internal static bool Extractor(string path, ref ShellLinkInfo info)
        {
            var lnk = new ShellLink(path);
            string exePath = lnk.GetPath();
            string extension = Path.GetExtension(exePath);
            if (extension != null && extension.ToUpper() != ".EXE") return false;
            info.Description = lnk.GetDescription();
            string exeName = Path.GetFileNameWithoutExtension(exePath);
            if (exeName != null) exeName = exeName.ToLower();
            string[] args = lnk.GetArguments().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            switch (exeName)
            {
                case "leproc":
                    info.ExeutionPath = GetLocaleEmulatorShortcutExecution(args);
                    break;
                case "apploc":
                    info.ExeutionPath = GetApplocaleShortcutExecution(args);
                    break;
                case "ntleac":
                    info.ExeutionPath = GetApplocaleShortcutExecution(args); //Current the method is same
                    break;
                case "ntleas":
                    info.ExeutionPath = GetApplocaleShortcutExecution(args); //Current the method is same
                    break;
                case "lsc":
                    info.ExeutionPath = GetApplocaleShortcutExecution(args); //Current the method is same
                    break;
                default:
                    info.ExeutionPath = exePath;
                    break;
            }
            int i;
            info.IconPath = lnk.GetIconLocation(out i);
            info.IconIndex = i;
            if (string.IsNullOrWhiteSpace(info.IconPath))
            {
                info.IconPath = info.ExeutionPath;
                info.IconIndex = 0;
            }
            return true;
        }

        /// <summary>
        ///     Save the locale emulator launch shortcut
        /// </summary>
        /// <param name="info">Shell link info object</param>
        /// <param name="path">Path to save</param>
        /// <param name="le">Path of Locale Emulator</param>
        /// <param name="guid">Profile guid</param>
        /// <returns></returns>
        internal static bool SaveShortcut(ShellLinkInfo info, string path, string le, string guid)
        {
            if (string.IsNullOrWhiteSpace(info.ExeutionPath)) return false;
            try
            {
                var lnk = new ShellLink();
                lnk.SetPath(le);
                lnk.SetArguments(string.Format(" -runas \"{0}\" \"{1}\"", guid, info.ExeutionPath));
                if (!string.IsNullOrWhiteSpace(info.Description)) lnk.SetDescription(info.Description);
                lnk.SetIconLocation(info.IconPath, info.IconIndex);
                lnk.Save(path);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        internal static string GetLocaleEmulatorShortcutExecution(string[] args)
        {
            return args.Last().Replace("\"", "");
        }

        internal static string GetApplocaleShortcutExecution(string[] args)
        {
            return args.First().Replace("\"", "");
        }
    }
}